﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;

namespace S7.Windows.Menu
{
    /// <summary>
    /// Interaction logic for EditMenu.xaml
    /// </summary>
    public partial class EditMenu : Window
    {
        public EditMenu()
        {
            InitializeComponent();
        }

        private void lbCategories_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lbCategories.SelectedItem != null)
            {
                string selectedMenu = lbCategories.SelectedItem.ToString();
                Thread thr = new Thread(new ThreadStart(() =>
                {
                    try
                    {
                        while (true)
                        {
                            if (MenuItem.lstItems != null)
                            {
                                if (selectedMenu == "All")
                                {
                                    lbMenuItems.Dispatcher.Invoke(new ThreadStart(() =>
                                    {
                                        try
                                        {
                                            lbMenuItems.ItemsSource = MenuItem.lstItems;
                                        }
                                        catch (Exception ex)
                                        {
                                            MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                                        }
                                    }), null);
                                    break;
                                }
                                else
                                {
                                    var query = from menu in Menus.lstMenus
                                                where menu.MenuName == selectedMenu
                                                select menu;
                                    List<Menus> SelectedMenu = query.ToList();
                                    int currentMenuID = SelectedMenu[0].MenuID;

                                    var items = from item in MenuItem.lstItems
                                                where item.MenuID == currentMenuID
                                                select item;
                                    lbMenuItems.Dispatcher.Invoke(new ThreadStart(() =>
                                    {
                                        try
                                        {
                                            lbMenuItems.ItemsSource = items;
                                        }
                                        catch (Exception ex)
                                        {
                                            MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                                        }
                                    }), null);
                                    break;
                                }
                            }
                            else
                            {
                                Thread.Sleep(100);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                    }
                }));
                thr.IsBackground = true;
                thr.Start();
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Thread thr = new Thread(new ThreadStart(() =>
            {
                try
                {
                    Menus.lstMenus = Menus.Search().ToList();
                    lbCategories.Dispatcher.Invoke(new ThreadStart(() =>
                    {
                        try
                        {
                            lbCategories.Items.Add("All");
                            foreach (var item in Menus.lstMenus)
                            {
                                lbCategories.Items.Add(item.MenuName);
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                    }), null);
                    List<MenuItem> lstItems = MenuItem.Search();
                    MenuItem.lstItems = new List<MenuItem>();
                    foreach (var item in lstItems)
                    {
                        MenuItem.lstItems.Add(item);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }));
            thr.IsBackground = true;
            thr.Start();
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {

             //lbMenuItems.SelectedItem
            var w = new EditMenuItem();
             w.ShowDialog();
        }

        private void btnRemove_Click(object sender, RoutedEventArgs e)
        {
            MenuItem menuItem = (MenuItem)lbMenuItems.SelectedValue;
            string text = tbSearch.Text;
            Thread thrDel = new Thread(new ThreadStart(() =>
                {
                    try
                    {
                        menuItem.Delete();
                        MenuItem.lstItems = null;
                        RefreshItems();
                        Search(text);
                        MessageBox.Show("Item is deleted!", "Done!", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                    }
                }));
            thrDel.IsBackground = true;
            thrDel.Start();
        }

        private void btnRemove_Copy_Click(object sender, RoutedEventArgs e)
        {
            if (lbCategories.SelectedIndex != 0)
            {
                string name = lbCategories.SelectedItem.ToString();
                Thread thr = new Thread(new ThreadStart(() =>
                    {
                        try
                        {
                            if (!name.Equals(""))
                            {
                                string error = "Deleting this menu, will also delete all sub items \n Are you sure you want to delete this Menu?";
                                MessageBoxResult result = MessageBox.Show(error, "Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Information, MessageBoxResult.Yes);
                                if (result == MessageBoxResult.Yes)
                                {
                                    var selctedItems = from menu in Menus.lstMenus
                                                       where menu.MenuName == name
                                                       select menu;
                                    foreach (var menu in selctedItems)
                                    {
                                        menu.Delete();
                                    }
                                    Menus.lstMenus = null;
                                    MenuItem.lstItems = null;
                                    RefreshMenu();
                                    lbCategories.Dispatcher.Invoke(new ThreadStart(() =>
                                    {
                                        try
                                        {
                                            while (true)
                                            {
                                                if (Menus.lstMenus != null)
                                                {
                                                    lbCategories.Items.Clear();
                                                    lbCategories.Items.Add("All");
                                                    foreach (var item in Menus.lstMenus)
                                                    {
                                                        lbCategories.Items.Add(item.MenuName);
                                                    }
                                                    lbMenuItems.Dispatcher.Invoke(new ThreadStart(() =>
                                                        {
                                                            try
                                                            {
                                                                Search(tbSearch.Text);
                                                            }
                                                            catch (Exception ex)
                                                            {
                                                                MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                                                            }
                                                        }), null);
                                                    break;
                                                }
                                                else
                                                {
                                                    Thread.Sleep(50);
                                                }
                                            }
                                        }
                                        catch (Exception ex)
                                        {
                                            MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
                                        }
                                    }), null);
                                }
                            }
                            else
                            {
                                MessageBox.Show("No Menu is selected", "Error!", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                        }
                    }));
                thr.IsBackground = true;
                thr.Start(); 
            }
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            var w = new AddItem();
            w.ShowDialog();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Menus.lstMenus = null;
            MenuItem.lstItems = null;
        }

        private void tbSearch_KeyUp(object sender, KeyEventArgs e)
        {
            string text = tbSearch.Text;
            Search(text);
        }

        public static void RefreshItems()
        {
            Thread thr = new Thread(new ThreadStart(() =>
                {
                    try
                    {
                        List<MenuItem> lstItems = MenuItem.Search();
                        MenuItem.lstItems = new List<MenuItem>();
                        foreach (var item in lstItems)
                        {
                            MenuItem.lstItems.Add(item);
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                    }
                }));
            thr.IsBackground = true;
            thr.Start();
        }

        public static void RefreshMenu()
        {
            Thread thr = new Thread(new ThreadStart(() =>
                {
                    try
                    {
                        Menus.lstMenus = Menus.Search();
                        RefreshItems();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                    }
                }));
            thr.IsBackground = true;
            thr.Start();
        }

        private void Search(string text)
        {
            if (text == "")
            {
                lbMenuItems.ItemsSource = MenuItem.lstItems;
            }
            else
            {
                Thread thrSearch = new Thread(new ThreadStart(() =>
                {
                    while (true)
                    {
                        if (MenuItem.lstItems == null)
                        {
                            Thread.Sleep(50);
                        }
                        else
                        {
                            List<MenuItem> lstItems = MenuItem.TextSearch(text);
                            lbMenuItems.Dispatcher.Invoke(new ThreadStart(() =>
                            {
                                lbMenuItems.ItemsSource = lstItems;
                            }), null);
                            break;
                        }
                    }
                }));
                thrSearch.IsBackground = true;
                thrSearch.Start();
            }
        }

        
    }
}
